#include <iostream>
#include <fstream>

#define REMOVE_BLOCK 0
#define REMOVE_LINE  1
#define KEEP         2

int
main(int argc, char* argv[])
{
  if (argc != 2)
    {
      std::cerr << "Usage: " << argv[0] << " <FILE>" << std::endl;
      return 1;
    }

  std::ifstream input(argv[1]);
  if (!input.is_open())
    {
      std::cerr << "File " << argv[1] << "does not exist!" << std::endl;
      return 1;
    }

  std::string output_filename = "out_";
  output_filename += argv[1];
  std::ofstream output(output_filename.c_str());

  std::size_t mode = KEEP;
  char ch;
  char next_ch;

  input.get(ch);

  while (input)
    {


      std::cerr << "Got ch = " << ch << ". Current mode = " << mode << std::endl;

      if (mode == KEEP)
	{
	  if (ch == '/')
	    {
	      input.get(next_ch);
	      std::cerr << "Got next_ch = " << next_ch << "  Current mode = " << mode << std::endl;

	      if (next_ch == '*')
		{
		  mode = REMOVE_BLOCK;
		}
	      else if (next_ch == '/')
		{
		  mode = REMOVE_LINE;
		}
	      else
		{
		  output << ch;
		  output << next_ch;
		}
	    }
	  else 
	    {
	      output << ch;
	    }
	}
      else if (mode == REMOVE_BLOCK)
	{
	  if (ch == '*')
	    {
	      input.get(next_ch);
	      std::cerr << "Got next_ch = " << next_ch << ". Current mode = " << mode << std::endl;
	      if (next_ch == '/')
		{
		  mode = KEEP;
		}
	    }
	}
      else // mode == REMOVE_LINE
	{
	  if (ch == '\n')
	    {
	      mode = KEEP;
	    }
	}
      std::cerr << "Continue reading" << std::endl;
      input.get(ch);
    }
  
  output.close();
}
